package com.yixing.tech.entity.service.impl;

import org.apache.commons.lang3.StringUtils;
import com.yixing.tech.common.base.BaseMongoDao;
import com.yixing.tech.common.base.BusinessException;
import com.yixing.tech.common.base.Page;
import com.yixing.tech.common.base.PageRow;
import com.yixing.tech.common.enums.ActionType;
import com.yixing.tech.common.enums.ErrCode;
import com.yixing.tech.common.enums.ParamUIType;
import com.yixing.tech.entity.dao.ObjModelDao;
import com.yixing.tech.entity.po.ObjModel;
import com.yixing.tech.entity.service.IObjModelService;
import com.yixing.tech.common.utils.DataUtil;
import com.yixing.tech.common.vo.s5.ActionVO;
import com.yixing.tech.common.vo.s5.EventVO;
import com.yixing.tech.common.vo.s5.ObjModelVO;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yixing tech
 * at 2022/2/10
 * describe：
 */
@Service
public class ObjModelServiceImpl implements IObjModelService {

    @Resource
    private BaseMongoDao baseMongoDao;
    @Resource
    private ObjModelDao objModelDao;


    @Override
    public void add(ObjModelVO objModelVO) throws BusinessException {
        ObjModel model = DataUtil.o2o(objModelVO, ObjModel.class);
        baseMongoDao.save(model);
    }

    @Override
    public void batchAdd(List<ObjModelVO> list) throws BusinessException {

    }

    @Override
    public ObjModelVO queryById(String id) {
        return null;
    }

    @Override
    public List<ObjModelVO> queryByIds(List<String> ids) {
        return null;
    }

    @Override
    public ObjModelVO queryByCondition(ObjModelVO objModelVO) throws BusinessException {
        Criteria criteria = baseMongoDao.condition2Criteria(objModelVO);
        ObjModel model = baseMongoDao.queryOne(ObjModel.class, criteria);
        return DataUtil.o2o(model, ObjModelVO.class);
    }

    @Override
    public void update(ObjModelVO objModelVO) throws BusinessException {

    }

    @Override
    public Page<ObjModelVO> list(PageRow<ObjModelVO> pageRow) {
        return null;
    }

    @Override
    public List<ObjModelVO> list(ObjModelVO condition) {
        return null;
    }

    @Override
    public void deleteById(String id) throws BusinessException {

    }

    @Override
    public void delete(List<String> ids) throws BusinessException {

    }

    @Override
    public void insertAction(ActionVO actionVO) throws BusinessException {
        if (StringUtils.isEmpty(actionVO.getModelId())) throw new BusinessException(ErrCode.PARAMETER_ERROR);
        objModelDao.addAction(actionVO);
    }

    @Override
    public void insertEvent(EventVO eventVO) throws BusinessException {
        if (StringUtils.isEmpty(eventVO.getModelId())) throw new BusinessException(ErrCode.PARAMETER_ERROR);
        objModelDao.addEvent(eventVO);
    }

    @Override
    public List<ActionVO> getActions(String productId) throws BusinessException {
        List<ActionVO> list = new ArrayList<>();
        ObjModel objModel = objModelDao.getManualAction(productId);
        if (objModel == null || objModel.getActions() == null) return list;
        for (ObjModel.Action action : objModel.getActions()) {
            if (!ActionType.option.name().equals(action.getType())) continue;
            List<ObjModel.Param> params = action.getParams();
            int size = params.size();
            for (int i = size - 1; i >= 0; i--) {
                if (ParamUIType.none.name().equals(params.get(i).getUiType())) {
                    params.remove(i);
                }
            }
            list.add(DataUtil.o2o(action, ActionVO.class));
        }
        return list;
    }

    @Override
    public ActionVO getAction(String productId, String funCode, String code) throws BusinessException {
        return objModelDao.getAction(productId, funCode, code);

    }
}
